package com.example.calendar.ui.theme

import android.content.Context
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

/**
 * Manages the application theme settings and provides functionality
 * to read and update theme preferences.
 */
class ThemeManager(private val context: Context) {
    
    companion object {
        private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "theme_preferences")
        private val DARK_THEME_KEY = booleanPreferencesKey("dark_theme")
        private val THEME_MODE_KEY = stringPreferencesKey("theme_mode")
    }
    
    /**
     * Theme mode options
     */
    enum class ThemeMode {
        SYSTEM, // Follow system theme
        LIGHT,  // Always light theme
        DARK    // Always dark theme
    }
    
    /**
     * Get the current theme mode preference
     */
    val themeMode: Flow<ThemeMode> = context.dataStore.data.map { preferences ->
        val themeModeString = preferences[THEME_MODE_KEY] ?: ThemeMode.SYSTEM.name
        try {
            ThemeMode.valueOf(themeModeString)
        } catch (e: IllegalArgumentException) {
            ThemeMode.SYSTEM
        }
    }
    
    /**
     * Get the current dark theme preference
     * This is used for backward compatibility before theme mode was introduced
     */
    val isDarkTheme: Flow<Boolean> = context.dataStore.data.map { preferences ->
        preferences[DARK_THEME_KEY] ?: false
    }
    
    /**
     * Set the theme mode
     */
    suspend fun setThemeMode(mode: ThemeMode) {
        context.dataStore.edit { preferences ->
            preferences[THEME_MODE_KEY] = mode.name
        }
    }
    
    /**
     * Set the dark theme preference
     * This is used for backward compatibility
     */
    suspend fun setDarkTheme(isDark: Boolean) {
        context.dataStore.edit { preferences ->
            preferences[DARK_THEME_KEY] = isDark
        }
    }
}

/**
 * A composable function that determines if dark theme should be used based on user preferences
 * and system settings.
 */
@Composable
fun shouldUseDarkTheme(themeManager: ThemeManager): Boolean {
    val themeMode by themeManager.themeMode.collectAsState(initial = ThemeManager.ThemeMode.SYSTEM)
    val isDarkTheme by themeManager.isDarkTheme.collectAsState(initial = false)
    
    return when (themeMode) {
        ThemeManager.ThemeMode.SYSTEM -> isSystemInDarkTheme()
        ThemeManager.ThemeMode.DARK -> true
        ThemeManager.ThemeMode.LIGHT -> false
    }
} 